﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.CRM.Bussiness;
using ZX.BLL.Extend;
using ZX.CRM.Library;
using ZX.Web.UI.Controls;
using ZX.EHR.Library;
using Newtonsoft.Json;
using System.Text;
using ZX.BLL;
using ZX.SYS.Library;

namespace ZX.WebApp.Crm.Report
{
    public partial class RptCrmValueList : ListPage
    {
        string strNoData = "";
        BTCrmCustomer bll = new BTCrmCustomer();
        private string ActionStr = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ActionStr = Request["Action"];
            if (ActionStr != null)
            {
                switch (ActionStr.ToLower())
                {
                    case "inittree":
                        InitTree();
                        break;
                    case "loadtree":
                        LoadTree();
                        break;
                    case "loadcustom":
                        AddCustomerType();
                        break;
                }
            }
            if (!IsPostBack)
            {
                BaseBussnies deptUserPost = new BaseBussnies();
                deptUserPost.entity = new V_DatUserPostDept();
                IList<V_DatUserPostDept> list = (List<V_DatUserPostDept>)deptUserPost.GetEntitys<V_DatUserPostDept>("Userid='" + this.UserId.ToString() + "'");

                if (list == null || list.Count <= 0)
                {
                    if (ActionStr.ToLower() != "")
                    {
                        string str = GetRoleFunction();
                        if (str == "")
                        {
                            strNoData = "0";
                        }
                    }
                    else
                    {
                        strNoData = "0";
                    }
                }
            }
        }
        private string GetRoleFunction()
        {
            IList<ZX.EHR.Library.SysRoleFunction> listFunction = (new ZX.BLL.BUser()).GetRoleFunction(this.UserId, this.FunctionID);
            StringBuilder strCondition = new StringBuilder("");

            if (listFunction == null)
            {
                return "";
            }
            foreach (var entity in listFunction)
            {
                switch (entity.OperateRangeType.Trim())
                {
                    //只能查看本人数据
                    case "0":
                        break;
                    //可以看本部门的
                    case "1":
                        break;
                    // 全部数据
                    case "2":
                        if (this.EntityName == "V_CrmCustomerInfo")
                        {
                            strCondition.Append("all");
                        }
                        break;
                    //自定义部门条件
                    case "3":
                        break;
                    // 自定义条件数据
                    case "4":
                        break;
                    default:
                        break;
                }
            }
            return string.IsNullOrEmpty(strCondition.ToString()) ? "" : "(" + strCondition.ToString() + ")";
        }

        protected void InitTree()
        {
            BaseBussnies deptUserPost = new BaseBussnies();
            deptUserPost.entity = new V_DatUserPostDept();
            IList<V_DatUserPostDept> list = (List<V_DatUserPostDept>)deptUserPost.GetEntitys<V_DatUserPostDept>("Userid='" + this.UserId.ToString() + "'");
            if (list != null && list.Count > 0)
            {
                List<ZtreeHelper> listTree = new List<ZtreeHelper>();
                foreach (V_DatUserPostDept dpd in list)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = dpd.DeptID.ToString(),
                        pId = "001",
                        name = dpd.DeptName,
                        isParent = true,
                        open = true,
                        iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/3.png",
                        iconClose = "",
                        click = true,
                        key1 = "dept",
                        key2 = ""
                    };
                    listTree.Add(zh);
                }

                string json = string.Empty;
                if (list != null)
                    json = JsonConvert.SerializeObject(listTree);
                Response.Clear();
                Response.ContentType = "application/json";
                Response.Write(json);
                Response.End();
            }
            else
            {
                BaseBussnies deptUserPost1 = new BaseBussnies();
                deptUserPost1.entity = new V_DatUserPostDept();
                IList<V_DatUserPostDept> list1 = (List<V_DatUserPostDept>)deptUserPost1.GetEntitys<V_DatUserPostDept>("DeptID='FB282647-FFE0-440B-81B2-1466D3EAC417'");
                if (list1 != null && list1.Count > 0)
                {
                    List<ZtreeHelper> listTree1 = new List<ZtreeHelper>();
                    foreach (V_DatUserPostDept dpd1 in list1)
                    {
                        ZtreeHelper zh = new ZtreeHelper
                        {
                            id = dpd1.DeptID.ToString(),
                            pId = "001",
                            name = dpd1.DeptName,
                            isParent = true,
                            open = true,
                            iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/3.png",
                            iconClose = "",
                            click = true,
                            key1 = "dept",
                            key2 = ""
                        };
                        listTree1.Add(zh);
                    }

                    string json = string.Empty;
                    if (list1 != null)
                        json = JsonConvert.SerializeObject(listTree1);
                    Response.Clear();
                    Response.ContentType = "application/json";
                    Response.Write(json);
                    Response.End();
                }
                else
                {

                    ZtreeHelper zh = new ZtreeHelper
                    {
                        error = "true"
                    };

                    List<ZtreeHelper> listError = new List<ZtreeHelper>();
                    listError.Add(zh);

                    string json = string.Empty;
                    json = JsonConvert.SerializeObject(listError);
                    Response.Clear();
                    Response.ContentType = "application/json";
                    Response.Write(json);
                    Response.End();
                }
            }
        }

        protected void LoadTree()
        {
            string parentid = Request["id"];
            BaseBussnies deptDept = new BaseBussnies();
            deptDept.entity = new DatDepartment();
            IList<DatDepartment> list = (List<DatDepartment>)deptDept.GetEntitys<DatDepartment>("ParentID='" + parentid + "'");
            List<ZtreeHelper> listTree = new List<ZtreeHelper>();
            if (list != null && list.Count > 0)
            {
                //加载树节点的子节点
                foreach (DatDepartment dpd in list)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = dpd.DeptID.ToString(),
                        pId = parentid,
                        name = dpd.DeptName,
                        isParent = true,
                        open = true,
                        iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/3.png",
                        iconClose = "",
                        click = true,
                        key1 = "dept",
                        key2 = ""
                    };
                    listTree.Add(zh);
                }
            }

            //加载人员
            BaseBussnies deptUserPost = new BaseBussnies();
            deptUserPost.entity = new V_UserEmp();
            IList<V_UserEmp> IUserEmp = deptUserPost.GetEntitys<V_UserEmp>("deptid='" + parentid + "' and IsExpire=0");
            if (IUserEmp != null && IUserEmp.Count > 0)
            {
                for (int i = 0; i < IUserEmp.Count; i++)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = IUserEmp[i].UserID.ToString(),
                        pId = parentid,
                        name = IUserEmp[i].Name,
                        isParent = true,
                        open = true,
                        iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/useropen.gif",
                        iconClose = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/userclose.gif",
                        click = true,
                        key1 = "user",
                        key2 = IUserEmp[i].UserID.ToString()
                    };
                    listTree.Add(zh);
                }
            }

            string json = string.Empty;
            if (listTree != null)
                json = JsonConvert.SerializeObject(listTree);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(json);
            Response.End();
        }

        private void AddCustomerType()
        {
            string CustomUser = Request["CustomUser"];
            string parentid = Request["id"];
            BaseBussnies bllCustomerType = new BaseBussnies();
            bllCustomerType.entity = new DatPhraseDetail();
            BDatPhraseDetail bllDetail = new BDatPhraseDetail();
            IList<DatPhraseDetail> ListDetailType = bllDetail.GetDatPhraseDetailByClassValue("CustomerType");

            List<ZtreeHelper> listTree = new List<ZtreeHelper>();
            if (ListDetailType != null && ListDetailType.Count > 0)
            {
                for (int i = 0; i < ListDetailType.Count; i++)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = ListDetailType[i].PhraseID.ToString(),
                        pId = parentid,
                        name = ListDetailType[i].PhraseText,
                        isParent = false,
                        open = true,
                        icon = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/2.png",
                        click = true,
                        key1 = "custom",
                        key2 = "CustomUser='" + CustomUser + "' and TypeId='" + ListDetailType[i].PhraseID + "'"
                    };
                    listTree.Add(zh);
                }
            }

            string json = string.Empty;
            if (listTree != null)
                json = JsonConvert.SerializeObject(listTree);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(json);
            Response.End();
        }
        public string ReturnNoData
        { get { return strNoData; } }
    }

}